<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>设备盘点</title>
    <link rel="stylesheet" href="/lib/layui/layui/css/layui.css?20190817.1">
    <link rel="stylesheet" href="/lib/layui/layui/css/common.css">
    <script src="/lib/layui/layui/layui.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/global.js"></script>
    <script type="text/javascript" src="../../js/jquery/jquery-1.9.1.js"></script>
    <script src="../../js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/layui/layui/js/baseCode/base.js?v=201908121454" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/layui/layui/js/common.js?v=201907231346" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/pagination/js/jquery.pagination.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../lib/ueditor/ueditor.config.js" type="text/javascript" charset="utf-8"></script>
    <script src="../lib/ueditor/ueditor.all.js" type="text/javascript" charset="utf-8"></script>
    <style>
        .mbox {
            padding: 8px;
        }
        .inbox {
            padding: 5px;
            padding-right: 30px;
        }
        .layui-btn {
            margin-left: 10px;
        }
        .layui-btn .layui-icon {
            margin-right: 0px;
        }
        .red {
            color: red;
            font-size: 16px;
        }
        .layui-form-label {
            padding: 8px 15px;
        }
        .layui-card-body {
            display: flex;
            padding: 0px;
        }
        .layui-lf {
            min-width: 10%;
            overflow-x: auto;
        }
        .layui-rt {
            width: 100%;
        }
        .layui-form-select .layui-input{
            height: 32px;
        }
        .layui-tab-title{
            height: 32px;
        }

        .layui-tab-title li{
            height: 33px;
            line-height: 33px;
        }
        .layui-tab layui-tab-card{
            margin-top: -4px;
        }
        .layui-tab-card>.layui-tab-title .layui-this:after {
            border-width: 0px;
        }
        .layui-table{
            margin-top: -2px !important;
        }
        .layui-table-view .layui-table td, .layui-table-view .layui-table th{
            padding: 2px 0;
        }
        #infoId td{
            padding: 5px 2px;
        }
        .tablecolor{
            background-color:#f2f2f2;
        }
        .layui-table{
            width:100%!important;
        }
        .bg{
            background-color: #F2F2F2;
        }
    </style>
</head>
<body>
<div class="mbox">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-rt">
                <table class="layui-table" lay-skin="line" id="Settlement" lay-size="sm" lay-filter="SettlementFilter"></table>
                <div class="layui-tab layui-tab-card" lay-filter="bottomTab" style="position: relative;z-index: 0;">
                    <ul class="layui-tab-title">
                        <li class="layui-this">盘点明细</li>
                    </ul>
                    <div class="layui-tab-content" style="padding: 0 10px">
                        <div class="layui-tab-item layui-show">
                            <table class="layui-table" id="transferDetail" lay-filter="transferFilter" lay-size="sm"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%--设备盘点新增/批量删除/查询字段--%>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add" style="width: 100px" id="add">新建盘点计划</button>
        <%--<button class="layui-btn layui-btn-sm" lay-event="submit" style="width: 100px">复制盘点计划</button>--%>
        <button class="layui-btn layui-btn-sm" lay-event="del" style="width: 70px">删除</button>
    </div>
    <%--<div class="demoTable" style="position:absolute;right: 8%;top: 10px;width: 600px;height: 30px">--%>
        <%--<label style="float: left;width: 60px;">查询字段</label>--%>
        <%--<div class="layui-input-inline" style="float:left;">--%>
            <%--<select name="modules" lay-verify="required" lay-filter="serachInfo"lay-search="" placeholder="请选择" style="height: 10px">--%>
                <%--<option value="0">请选择查询字段</option>--%>
                <%--<option value="CUSTOMER_NAME">客户名称</option>--%>
                <%--<option value="CUSTOMER_ADDRESS">地址</option>--%>
                <%--<option value="INDUSTRY_CODE">所属行业</option>--%>
                <%--<option value="CREDIT_CODE">统一社会信用代码</option>--%>
                <%--<option value="REGIST_PERIOD">登记时间</option>--%>
                <%--<option value="URL">网址</option>--%>
                <%--<option value="PHONE_NUMBER">电话号码</option>--%>
                <%--<option value="MEMO">备注</option>--%>
            <%--</select>--%>
        <%--</div>--%>
        <%--<div style="float: left">--%>
            <%--<div class="layui-inline">--%>
                <%--<input class="layui-input" name="searchtext"  autocomplete="off" style="height: 32px;margin-left: 6px;">--%>
            <%--</div>--%>
            <%--<button class="layui-btn" data-type="reload"   lay-event="searchCust" style="height:30px;line-height: 30px; ">搜索</button>--%>
        <%--</div>--%>
    <%--</div>--%>
</script>

<%--设备盘点明细下头部--%>
<script type="text/html" id="toolbars">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="synbmit" style="width: 70px">同步</button>
    </div>
</script>
<%--设备盘点上头部--%>
<script type="text/html" id="barOperation">
    <a class="layui-btn  layui-btn-xs" lay-event="sub" id="sub">盘点</a>
    <a class="layui-btn  layui-btn-xs" lay-event="edit" id="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="barOperation1">
    <a class="layui-btn layui-btn-xs" lay-event="pd" hide="true">盘点</a>
</script>
<script type="text/javascript">

    var settlementTable;
    var deptId;
    var addId;
    var checkobj;
    var syndata;

    layui.use(['table', 'layer','laydate', 'form', 'element'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var element = layui.element;
        var laydate = layui.laydate;

        //设备盘点表格实例
        settlementTable = table.render({
            elem: '#Settlement'
            , page: true //开启分页
            , url: '/equipInventory/InvenPlanList'//数据接口
            , toolbar: '#toolbar'
            , cols: [[ //标题栏
                {type: 'checkbox'}
                , {field: 'inveName', title: '盘点名称'}
                , {field: 'inveId', title: '盘点单号'}
                , {field: 'checkTimePlan', title: '计划盘点时间',templet: function(d) {
                        return format(d.checkTimePlan)
                    }}
                , {field: 'checkUser', title: '盘点人',width:100}
                , {field: 'deptName', title: '盘点部门'}
                , {field: 'laveCheck', title: '剩余未盘数',width:100}
                , {field: 'memo', title: '备注'}
                , {field: 'inveStatus', title: '流水号'}
                , {field: 'inveStatus', title: '盘点状态',width:100,templet: function(d) {
                       if(d.inveStatus == 0) return '待盘点'
                       if(d.inveStatus == 1) return '部分盘点'
                       if(d.inveStatus == 2) return '已盘点'
                    }}
                , {width: 200, title: '操作', align: 'center',templet: function(d) {
                        if(d.inveStatus == 0){
                            return '    <a class="layui-btn  layui-btn-xs" lay-event="sub" id="sub">盘点 </a>\n' +
                                '    <a class="layui-btn  layui-btn-xs" lay-event="edit" id="edit">编辑</a>\n' +
                                '    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>'
                        }else if(d.inveStatus == 1){
                            return '    <a class="layui-btn  layui-btn-xs" lay-event="sub" id="sub">盘点 </a>\n';
                        } else {
                            return '    <a class="layui-btn  layui-btn-xs" lay-event="subdetail" id="subdetail">盘点详情</a>\n';
                        }

                    }}
            ]]
            , limit: 10
            ,done:function (res) {
                if(res.data.length >0){
                    var ownerlogId = res.data[0].inveId;
                    equipTable("#transferDetail",ownerlogId,"#toolbars");
                    checkobj = res.data[0].limsEquipInveCont;
                }else{
                    cequipTable("#transferDetail","#toolbars");
                }
            }
        })

        // 设备盘点头部事件
        table.on('toolbar(SettlementFilter)', function (obj) {
            var layEvent = obj.event;
            switch (layEvent) {
                case 'submit':
                    var subdata = table.checkStatus('Settlement').data;
                    break;
                case 'add':
                    layer.open({
                        type: 2,
                        skin: false, //加上边框
                        area: ['75%', '90%'], //宽高
                        title: '添加盘点计划',
                        maxmin: true,
                        btn: ["确定", "取消"],
                        content: '/equipInventory/addInventory?begin=add',
                        yes: function (index, layero) {
                            var jsondata = $(layero).find("iframe")[0].contentWindow.addjData();
                            var ownerList = JSON.stringify(jsondata);
                            var ownerLog = $(layero).find("iframe")[0].contentWindow.addData();
                            var checkedLog = $(layero).find("iframe")[0].contentWindow.addchecked();
                            if (jsondata.length == 0 ||  ownerLog.inveName == "" || ownerLog.checkTimePlan == "") {
                                layer.msg("请填写完整的信息添加！");
                                return false;
                            } else {
                                layer.close(index);
                                $.ajax({
                                    type: "post",
                                    url: "/equipInventory/addInvenPlan",
                                    dataType: "json",
                                    data: {
                                        limsEquipInvePlan:JSON.stringify(ownerLog),
                                        limsEquipInveCont:JSON.stringify(checkedLog),
                                        equips:ownerList
                                    },
                                    success: function (res) {
                                        if (res.code == 0) {
                                            layer.msg(res.msg);
                                            settlementTable.reload();
                                        } else layer.msg(res.msg);
                                    }
                                })

                            }
                        }
                    });
                    break;
                case 'del':
                    var deldata = table.checkStatus('Settlement').data;
                    var logIds = ""
                    for (var i = 0; i < deldata.length; i++) {
                        logIds += deldata[i].inveId + ",";
                    }
                    if (deldata.length == 0 || deldata.length < 0) {
                        layer.msg("请选择一条或者多条进行删除。");
                        return false;
                    } else if (deldata.length > 0) {
                        publicDelete('/equipInventory/delInveInfo', logIds, settlementTable);
                    }
                    break;
            }
        });

        // 设备盘点/编辑/删除操作
        table.on('tool(SettlementFilter)', function (obj) {
            //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            var ids = data.applicId+",";
            if (layEvent === 'sub') {
                layer.confirm('全部盘点?', {icon: 3, title:'提示'}, function(index){
                    $.post('/equipInventory/upInveStatus',{planStatus:2,planId:data.inveId},function (res) {
                        layer.msg(res.msg)
                        settlementTable.reload();
                    })
                    layer.close(index)
                });

            } else if (layEvent === 'del') {
                publicDelete('/equipInventory/delInveInfo', data.inveId, settlementTable);
            } else if (layEvent === 'edit') { //编辑
                layer.open({
                    type: 2,
                    skin: false, //加上边框
                    area: ['85%', '60%'], //宽高
                    title: '新增申请',
                    maxmin: true,
                    btn: ["确定", "取消"],
                    content: '/equipInventory/addInventory?begin=edit&inveId='+data.inveId,
                    yes: function (index, layero) {
                        var jsondata = $(layero).find("iframe")[0].contentWindow.addjData();
                        var contId = $(layero).find("iframe")[0].contentWindow.contId;
                        var ownerLog = $(layero).find("iframe")[0].contentWindow.addData();
                        ownerLog.inveId = data.inveId
                        var checkedLog = $(layero).find("iframe")[0].contentWindow.addchecked();
                        checkedLog.contId = contId
                        if (ownerLog.inveName == "" || ownerLog.checkTimePlan == "") {
                            layer.msg("请填写完整的信息添加！");
                            return false;
                        } else {
                            layer.close(index);
                            $.ajax({
                                type: "post",
                                url: "/equipInventory/editInveInfo",
                                dataType: "json",
                                data: {
                                    limsEquipInvePlan:JSON.stringify(ownerLog),
                                    limsEquipInveCont:JSON.stringify(checkedLog),
                                },
                                success: function (res) {
                                    if (res.code == 0) {
                                        layer.msg(res.msg);
                                        settlementTable.reload();
                                    } else layer.msg(res.msg);
                                }
                            })

                        }
                    }
                });
            }else if(layEvent === 'subdetail'){
                layer.open({
                    type: 2,
                    skin: false, //加上边框
                    area: ['75%', '60%'], //宽高
                    title: '详情',
                    maxmin: true,
                    btn: ["确定", "取消"],
                    content: '/equipInventory/addInventory?detail=detail&begin=edit&inveId='+data.inveId,
                    yes: function (index, layero) {
                     layer.close(index);
                    }
                });
            }
        });

        //设备盘点表格行事件
        table.on('row(SettlementFilter)', function (obj) {
            var events = obj.event;
            var data = obj.data;
            deptId = data.originalDeptId;
            addId = data.inveId;
            var index = $(this).attr('data-index');
            var tr = $(this).parent().children("tr");
            $(this).addClass("bg");
            for (var i = 0; i < tr.length; i++) {
                if (index != i) tr.eq(i).removeClass("bg");
            };
            equipTable('#transferDetail',data.inveId,"#toolbars")
        })

        // 设备盘点明细操作
        table.on('tool(transferFilter)', function (obj) {
            //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'InventoryDetail') {
                layer.open({
                    type: 2,
                    skin: false, //加上边框
                    area: ['98%', '98%'], //宽高
                    title: '盘点详情',
                    maxmin: true,
                    btn: ["确认", "取消"],
                    content: '/equipInventory/editInventory?status=0&detailId='+data.detailId+'&inveId='+data.inveId,
                    yes: function (index, layero) {
                        layer.close(index);
                    }
                });

            } else if (layEvent === 'del') {
                publicDelete('/equipInventory/delInveInfo', data.inveId, settlementTable);
            } else if (layEvent === 'Inventory') { //盘点
                layer.open({
                    type: 2,
                    skin: false, //加上边框
                    area: ['55%', '75%'], //宽高
                    title: '盘点',
                    maxmin: true,
                    btn: ["确认盘点", "取消"],
                    content: '/equipInventory/editInventory?status=1&detailId='+data.detailId+'&inveId='+data.inveId
                    , success: function (res) {},
                    yes: function (index, layero) {
                        var ownerLog = $(layero).find("iframe")[0].contentWindow.addData();
                        // checkedLog.contId = contId
                        layer.close(index);

                            $.ajax({
                                type: "post",
                                url: "/equipInventory/upInveStatus",
                                dataType: "json",
                                data: {
                                    planStatus:1,
                                    planId:data.inveId,
                                    limsEquipInventory:JSON.stringify(ownerLog),
                                },
                                success: function (res) {
                                    if (res.code == 0) {
                                        layer.msg(res.msg);
                                        settlementTable.reload();
                                    } else {
                                        layer.msg(res.msg);
                                    }
                                }
                            })

                    }
                });
            }
        });

        // 设备盘点明细头部事件操作
        table.on('toolbar(transferFilter)', function (obj) {
            var layEvent = obj.event
            syndata=table.checkStatus('transferDetail').data
            console.log(syndata)
            switch (layEvent) {
                case 'synbmit':
                    layer.confirm('是否同步设备?', {icon: 3, title:'提示'}, function(index){
                        $.post('/equipInventory/syncEquip',{equipLists:JSON.stringify(syndata)},function (res) {
                            layer.msg(res.msg)
                            settlementTable.reload();
                        })
                        layer.close(index)
                    });
                    break;
            }
        });

    });


    //设备盘点明细表格实例
    function equipTable(gid,logid,tid) {
        layui.table.render({
            elem: gid
            ,data:[]
            , url: '/equipInventory/findEquipByInveId?inveId='+logid
            ,toolbar:tid
            // ,id:'equip'
            , page: true //开启分页
            , limit: 5 //每页默认显示的数量
            , limits: [5, 10, 15, 20, 25, 30]
            , cols: [[
                {type: 'checkbox'}
                , {field: 'equipName', title: '设备名称'}
                , {field: 'equipNo', title: '固定资产编号'}
                , {field: 'factoryNo', title: '出厂编号'}
                , {field: 'modelNo', title: '型号'}
                , {field: 'checkUser', title: '盘点人'}
                , {field: 'checkTime', title: '盘点时间',templet: function(d) {
                        return format(d.checkTime)
                    }}
                ,{field: 'whetherChanges', title: '是否变动',width:100}
                , {field: 'inveStatus', title: '盘点状态',width:100,templet: function(d) {
                        if(d.inveStatus == 0) return '待盘点'
                        if(d.inveStatus == 1) return '已盘点'
                    }}
                , {width: 200, title: '操作', align: 'center',width:110,templet: function(d){
                        if(d.inveStatus == 0){
                            return '<a data-type="1" class="layui-btn layui-btn-xs" lay-event="Inventory">盘点</a>'
                        }else if(d.inveStatus == 1){
                            return '<a data-type="1" class="layui-btn layui-btn-xs" lay-event="InventoryDetail">盘点详情</a>'
                        }
                    }}

            ]]
            ,done:function (res) {
                syndata = res.data
                console.log(syndata)
            }
        });
    }

    //将毫秒数转为yyyy-MM-dd格式时间
    function format(t) {
        if(t) return new Date(t).Format("yyyy-MM-dd");
        else return ''
    }

</script>
</body>
</html>
